package com.lcz.audioplayer;

import android.media.MediaPlayer;
import android.util.Log;
import android.widget.Toast;

import com.lcz.audioplayer.bean.AudioBean;
import com.lcz.audioplayer.cache.HttpProxyCacheUtil;
import com.lcz.audioplayer.service.PlayerService;
import com.lcz.audioplayer.util.ApplicationUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:     刘传政
 * 创建时间:  15:57 2024/4/9 0009
 * QQ:      1052374416
 * 电话:     18501231486
 * 描述:     播放核心类。用户只需要跟此类打交道
 */
public class PlayerClient implements IPlayerClient {
    private List<PlayerManagerCallback> mListener = new ArrayList<>();
    private static PlayerClient sPlayerClient;
    private PlayerManager playerManager;
    private ArrayList<AudioBean> voiceList;
    private int currentIndex = 0;

    public static PlayerClient getInstance() {
        if (sPlayerClient == null) {
            synchronized (PlayerClient.class) {
                if (sPlayerClient == null) {
                    sPlayerClient = new PlayerClient();
                }
            }
        }
        return sPlayerClient;
    }

    private PlayerClient() {
        playerManager = new PlayerManager();
        playerManager.setCallBack(new PlayerManagerCallback() {
            @Override
            public void onProgressChange(int currentProgress, int total, String currentPositionStr, String totalDurationStr) {
                for (PlayerManagerCallback playerManagerCallback : mListener) {
                    playerManagerCallback.onProgressChange(currentProgress, total, currentPositionStr, totalDurationStr);
                }
            }

            @Override
            public void onPlayStart() {

                for (PlayerManagerCallback playerManagerCallback : mListener) {
                    playerManagerCallback.onPlayStart();
                }
            }

            @Override
            public void onPlayPause() {
                for (PlayerManagerCallback playerManagerCallback : mListener) {
                    playerManagerCallback.onPlayPause();
                }
            }

            @Override
            public void onPlayStop() {
                for (PlayerManagerCallback playerManagerCallback : mListener) {
                    playerManagerCallback.onPlayStop();
                }
            }

            @Override
            public void onPlayError(MediaPlayer mp, int what, int extra) {
                for (PlayerManagerCallback playerManagerCallback : mListener) {
                    playerManagerCallback.onPlayError(mp, what, extra);
                }
            }

            @Override
            public void onCompletion() {
                for (PlayerManagerCallback playerManagerCallback : mListener) {
                    playerManagerCallback.onCompletion();
                }
            }
        });
    }

    @Override
    public void play() {
        playByIndex(currentIndex);
    }

    @Override
    public void pause() {
        playerManager.pause();
    }

    @Override
    public void stop() {
        playerManager.stop();
    }

    @Override
    public void playPre() {
        //播放前一个节目
        if (currentIndex >= 1) {
            currentIndex = currentIndex - 1;
            playByIndex(currentIndex);
        } else {
            Toast.makeText(ApplicationUtil.getCurApplication(), "暂无", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void playNext() {
        //播放下一个节目
        if (currentIndex <= voiceList.size() - 1 - 1) {
            currentIndex = currentIndex + 1;
            playByIndex(currentIndex);
        } else {
            Toast.makeText(ApplicationUtil.getCurApplication(), "暂无", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public int getCurrentIndex() {
        return currentIndex;
    }

    @Override
    public ArrayList<AudioBean> getAudioList() {
        return voiceList;
    }


    @Override
    public void playByIndex(int index) {
        if (voiceList == null || voiceList.isEmpty() || index > (voiceList.size()-1)) {
            return;
        }
        PlayerService.startPlayerService();
        currentIndex = index;
        String proxyUrl = HttpProxyCacheUtil.getAudioProxy().getProxyUrl(voiceList.get(index).getPlay_url());
        Log.i("proxyUrl", proxyUrl);
        playerManager.start(proxyUrl);//使用缓存代理url
    }

    @Override
    public void seekTo(int progress) {
        playerManager.seekTo(progress);
    }

    @Override
    public boolean isPlaying() {
        return playerManager.isPlaying();
    }

    @Override
    public void setVoiceList(ArrayList<AudioBean> voiceList) {
        this.voiceList = voiceList;
    }


    @Override
    public void registerListener(PlayerManagerCallback callback) {
        if (!mListener.contains(callback)) {
            mListener.add(callback);
        }
    }

    @Override
    public void unRegisterListener(PlayerManagerCallback callback) {
        mListener.remove(callback);
    }

    @Override
    public int getState() {

        return playerManager.getState();
    }
}
